export function formatDate(date, fmt) {
	date = new Date(date);
	let ret;
	const opt = {
		"y+": date.getFullYear().toString(), // 年
		"M+": (date.getMonth() + 1).toString(), // 月
		"d+": date.getDate().toString(), // 日
		"h+": date.getHours().toString(), // 时
		"m+": date.getMinutes().toString(), // 分
		"s+": date.getSeconds().toString(), // 秒
		// 有其他格式化字符需求可以继续添加，必须转化成字符串
	};
	for (let k in opt) {
		ret = new RegExp("(" + k + ")").exec(fmt);
		if (ret) {
			fmt = fmt.replace(ret[1],
				(ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length,
					"0")));
		}
	}

	return fmt;
}

export function formatDay(date) {
	date = new Date(date);
	const dayList = ["日", "一", "二", "三", "四", "五", "六"];
	return dayList[date.getDay()];
}

export function formatNumber(n) {
	n = n.toString();
	return n[1] ? n : "0" + n;
}

export function formatMoney(val, opts = {}) {
	if (!val) {
		return ''
	}

	let
		fields = (val / 100 + '').split('.'),
		{ ignoreZeroAfterDot, prefix = '￥' } = opts

	if (fields.length === 1) {
		fields.push('00')
	}

	if (fields[1].length < 2) {
		fields[1] += '0'
	}

	if (typeof ignoreZeroAfterDot === 'undefined') {
		ignoreZeroAfterDot = true
	}

	const str = ignoreZeroAfterDot === true && fields[1] === '00' ? fields[0] : fields.join('.')
	return prefix + str
}
